package com.visionet.school.app.web.common.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {

	SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");

	public static Date getStartTimeOfDay(Date date){
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.set(Calendar.HOUR_OF_DAY, 0);
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.SECOND, 0);
		ca.set(Calendar.MILLISECOND, 0);
		return ca.getTime();
	}

	public static Date getEndTimeOfDay(Date date){
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.set(Calendar.HOUR_OF_DAY, 23);
		ca.set(Calendar.MINUTE, 59);
		ca.set(Calendar.SECOND, 59);
		ca.set(Calendar.MILLISECOND, 999);
		return ca.getTime();
	}

	/**
	 * 获取上周五时间
	 */
	public static Date lastFirday(int dataNumber) {

//　　   作用防止周日得到本周日期
		Calendar calendar = Calendar.getInstance();
//		calendar.set(2017,5,12);
		while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
			calendar.add(Calendar.DAY_OF_WEEK, -1);
		}
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		int offset = 7 - dayOfWeek;
//		calendar.add(Calendar.DATE, offset - 9);原代码，出现BUG，恢复此代码
		calendar.add(Calendar.DATE, offset - dataNumber);
		return getFirstDayOfWeek(calendar.getTime(), 6);//这是从上周日开始数的到本周五为6

	}



	/**
	 * 获取上周一时间
	 */
	public static Date lastMonday(int dateNumber) {
		Calendar calendar = Calendar.getInstance();
		while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
			calendar.add(Calendar.DAY_OF_WEEK, -1);
		}
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		int offset = 1 - dayOfWeek;
//		calendar.add(Calendar.DATE, offset - 7);//原代码。如有BUG开放此代码
		calendar.add(Calendar.DATE, offset - dateNumber);
		return getFirstDayOfWeek(calendar.getTime(), 2);
	}

	/**
	 * 根据时间传入得上（累加）时间
	 */



	/**
	 * 得到某一天的该星期的第一日 00:00:00
	 *
	 * @param date
	 * @param firstDayOfWeek
	 *            一个星期的第一天为星期几
	 *
	 * @return
	 */
	public static Date getFirstDayOfWeek(Date date, int firstDayOfWeek) {
		Calendar cal = Calendar.getInstance();
		if (date != null)
			cal.setTime(date);
		cal.setFirstDayOfWeek(firstDayOfWeek);//设置一星期的第一天是哪一天
		cal.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);//指示一个星期中的某天
		cal.set(Calendar.HOUR_OF_DAY, 0);//指示一天中的小时。HOUR_OF_DAY 用于 24 小时制时钟。例如，在 10:04:15.250 PM 这一时刻，HOUR_OF_DAY 为 22。
		cal.set(Calendar.MINUTE, 0);//指示一小时中的分钟。例如，在 10:04:15.250 PM 这一时刻，MINUTE 为 4。
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}



	private static int getMondayPlus() {
		Calendar cd = Calendar.getInstance();
		// 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
		int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
		if (dayOfWeek == 1) {
			return -6;
		} else {
			return 2 - dayOfWeek;
		}
	}
	// 获得当前周- 周一的日期
	public static  String getCurrentMonday() {
		int mondayPlus = getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus);
		Date monday = currentDate.getTime();
		DateFormat df = DateFormat.getDateInstance();
		String preMonday = df.format(monday);
		return preMonday;
	}

	// 获得当前周- 周五  的日期
	public static String getPreviousSunday() {
		int mondayPlus = getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus +4);
		Date monday = currentDate.getTime();
		DateFormat df = DateFormat.getDateInstance();
		String preMonday = df.format(monday);
		return preMonday;

	}

	//获取本月开始时间
	public String getCurrentMonthStartTime() {

		Calendar c = Calendar.getInstance();
		String now = null;
		try {
			c.set(Calendar.DATE, 1);
			now = shortSdf.format(c.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}
//	public static void main(String[] args) throws Exception {
//		DateUtils utils = new DateUtils();
//		//String time = utils.getCurrentMonthStartTime();
//		String time = utils.getBeforeLastMonthdate(0);
//		System.out.println(time);
//
//	}

	/**
	 * 获取当月结束时间
	 */
	public String getCurrentMonthEndTime() {
		Calendar c = Calendar.getInstance();
		String now = null;
		try {
			c.set(Calendar.DATE, 1);
			c.add(Calendar.MONTH, 1);
			c.add(Calendar.DATE, -1);
			now = shortSdf.format(c.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 当前年的开始时间
	 */
	public static String getCurrentYearStartTime() {
		Calendar c = Calendar.getInstance();
		String now = null;
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			c.set(Calendar.MONTH, 0);
			c.set(Calendar.DATE, 1);

			now = format.format(c.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 当前年的结束时间
	 */
	public static String getCurrentYearEndTime() {
		Calendar c = Calendar.getInstance();
		String now = null;
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			c.set(Calendar.MONTH, 11);
			c.set(Calendar.DATE, 31);
			now = format.format(c.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 获取上个月开始时间
	 */
	public static String getBeforeFirstMonthdate(int dateStartSize)throws Exception {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar=Calendar.getInstance();
//		calendar.add(Calendar.MONTH, -1);//-1代表上一个月，-2可以代表前2个月
		calendar.add(Calendar.MONTH, dateStartSize);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return format.format(calendar.getTime());
	}
	/**
	 * 获取上个月结束时间
	 */
	public static String getBeforeLastMonthdate(int dateEndSize)throws Exception {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar=Calendar.getInstance();
		int month=calendar.get(Calendar.MONTH);
//		calendar.set(Calendar.MONTH, month-1);
		calendar.set(Calendar.MONTH, month - dateEndSize);//-1代表上一个月，-2可以代表前2个月
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return format.format(calendar.getTime());
	}
}
